package cn.manjuu.touhou.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import cn.manjuu.touhou.APP;
import cn.manjuu.touhou.R;
import cn.manjuu.touhou.engine.BaseInfoEngine;
import cn.manjuu.touhou.engine.NavigationEngine;
import cn.manjuu.touhou.interfaces.OnBaseInfoChangeListener;
import cn.manjuu.touhou.manager.AssetsDatabaseManager;
import cn.manjuu.touhou.util.Looger;

public class MainActivity extends FragmentActivity implements OnClickListener,
		OnBaseInfoChangeListener {
	private Button btn_toy_machine;

	private Button btn_card_info;
	private Button btn_make_money;

	private TextView tv_player_money;

	private TextView tv_player_name;

	private Button btn_drama;

	private Button btn_ditu;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		findView();
		registerListener();
		init();
	}

	private void init() {
		AssetsDatabaseManager.initManager(getApplication()); 
		NavigationEngine.getInstance().setmActivity(this);

		NavigationEngine.getInstance().init();

		initPlayerInfo();
	}

	private void initPlayerInfo() {
		tv_player_name.setText(BaseInfoEngine.getInstance().getBaseInfo()
				.getNickName());
		tv_player_money.setText(BaseInfoEngine.getInstance().getBaseInfo()
				.getMoney()
				+ " G");
	}

	private void findView() {
		btn_toy_machine = (Button) findViewById(R.id.btn_toy_machine);
		btn_card_info = (Button) findViewById(R.id.btn_card_info);
		btn_make_money = (Button) findViewById(R.id.btn_make_money);
		tv_player_name = (TextView) findViewById(R.id.tv_player_name);
		tv_player_money = (TextView) findViewById(R.id.tv_player_money);

		btn_drama = (Button) findViewById(R.id.btn_drama);
		btn_ditu = (Button) findViewById(R.id.btn_ditu);
	}

	private void registerListener() {
		// TODO Auto-generated method stub
		btn_toy_machine.setOnClickListener(this);
		btn_card_info.setOnClickListener(this);
		btn_make_money.setOnClickListener(this);
		btn_drama.setOnClickListener(this);
		btn_ditu.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_toy_machine:
		case R.id.btn_card_info:
		case R.id.btn_make_money:
		case R.id.btn_ditu:
			NavigationEngine.getInstance().onClick(v);
			break;
		case R.id.btn_drama:
			NavigationEngine.getInstance().toDaramActivity("0");
			break;
		}
	}

	@Override
	public void onBaseInfoChangeListener() {
		// TODO Auto-generated method stub
		Looger.i(APP.MainActivity, "onBaseInfoChangeListener()");
		tv_player_money.setText(BaseInfoEngine.getInstance().getBaseInfo()
				.getMoney()
				+ " G");
	}
	
	@Override
	public void finish() {
		// TODO Auto-generated method stub
		super.finish();
		overridePendingTransition(R.anim.alpha_enter, R.anim.alpha_exit);
	}

}